package com.ymatou.iapi.productquery.testcase;

import com.ymatou.iapi.productprice.parameter.ProductAllPriceBean;
import com.ymatou.iapi.productquery.parameter.ProductList.ProductInListBean;
import com.ymatou.iapi.productquery.service.GetProductListByTradeIsolationCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductPriceCallService;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.ArrayList;
import java.util.List;

public class Ts_GetProductListByTradeIsolation {
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));
	private static GetProductListByTradeIsolationCall getProductListByTradeIsolationCall = new GetProductListByTradeIsolationCall();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("交易隔离查询接口,获取商品简单信息");
	}

	@Before
	public void caseUp() {
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductListByTradeIsolationCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductListByTradeIsolation_001() {
		Logger.start(true,"普通单规格商品，普通单规格直播商品 查询");
		Logger.comment("has been test in Tc_GetProductListByProductIdList_001");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductListByTradeIsolation_002() {
		Logger.start(true,"普通多规格商品,普通多规格直播商品 查询");
		Logger.comment("has been test in Tc_GetProductListByProductIdList_002");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductListByTradeIsolation_003() {
		Logger.start(true,"普通活动商品查询");
		Logger.comment("has been test in Tc_GetProductListByProductIdList_003");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductListByTradeIsolation_004() {
		Logger.start(true,"交易隔离活动商品查询");
		Logger.comment("has been test in Tc_GetProductListByProductIdList_004");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductListByTradeIsolation_005() {
		Logger.start(true,"交易隔离,新哈尼新人活动,IsNewBuyerActivity为true");
		try {
			ProductAllPriceBean productAllPriceBean = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndIsolation();
			String productId = productAllPriceBean.getProductId();
			int inaid = Integer.parseInt(new ActivityProductsWapper().getByProdId(productId).get("inaid").toString());

			List<String> prodList = new ArrayList<>();
			prodList.add(productId);
			checkByProductList(prodList,10,inaid,0,0);

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	public static void checkByProductList(List<String> prodList, int NextActivityExpire, int prodInActivityId, int liveId, int nextActivityId)
			throws Exception {
		YMTDateUtil.waitTime(2);
		List<ProductInListBean> tgtProductInListBeanList = new ArrayList<>();
		prodList.forEach(ele -> {
			try {
				ProductInListBean productInListBean = ProductQueryCallService.getProductInListBean(ele, prodInActivityId, liveId);
				productInListBean.setNextActivityId(nextActivityId);
				tgtProductInListBeanList.add(productInListBean);
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

		JSONObject ret;
		String chkdesc;
		// 交易隔离接口
		ret = getProductListByTradeIsolationCall.search(prodList, NextActivityExpire);
		chkdesc = "检查 GetProductListByTradeIsolation";
		// 返回码检查
		Logger.verifyEquals(200, ret.getInt("Code"), "返回码检查");

		// 返回值取数据
		List<ProductInListBean> actProductInListBeanList = new ArrayList<>();
		JSONArray prodListAry = ret.getJSONObject("Data").getJSONArray("ProductList");
		for (int i = 0; i < prodListAry.length(); i++) {
			actProductInListBeanList.add(ProductQueryCallService.getBeanFromJson(prodListAry.getJSONObject(i), ProductInListBean.class));
		}
		boolean isNewBuyerActivity = prodListAry.getJSONObject(0).getBoolean("IsNewBuyerActivity");
		Logger.verifyEquals(true,isNewBuyerActivity,"验证IsNewBuyerActivity");
		// 比对
		ProductQueryCallService.compareProductInListBean(prodList, tgtProductInListBeanList, actProductInListBeanList, chkdesc);

	}
	
}
